package com.example.backend.controller;

import com.example.backend.entity.R;
import com.example.backend.entity.User;
import com.example.backend.service.IUserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl.it
 * @since 2024-12-15
 */
@RestController
//@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
//
    @PostMapping("/login")
    public R login(HttpSession session, @RequestBody User user){

//        return R.data(user);
        User userInfo = userService.lambdaQuery()
                .eq(User::getName, user.getName())
                .eq(User::getPassword, user.getPassword()).one();
        if (Objects.isNull(userInfo)){
            return R.error("登录失败");
        }
        //c存储数据标识
        session.setAttribute("userInfo",userInfo);
        user.setPassword(null);
        return R.data(userInfo);
    }

}
